package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.entity.Panorama;
import cn.com.cifi.mars.entity.PanoramaNode;
import cn.com.cifi.mars.entity.PanoramaNodeDeliverable;
import cn.com.cifi.mars.mapper.mars.PanoramaMapper;
import cn.com.cifi.mars.service.PanoramaNodeDeliverableService;
import cn.com.cifi.mars.service.PanoramaNodeService;
import cn.com.cifi.mars.service.PanoramaService;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.UUIDUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 全景计划主表 服务实现类
 * </p>
 *
 * @author plx
 * @since 2020-02-03
 */
@Service
public class PanoramaServiceImpl extends ServiceImpl<PanoramaMapper, Panorama> implements PanoramaService {

	@Autowired
	private PanoramaMapper panoramaMapper;
	@Autowired
	private PanoramaNodeService panoramaNodeService;
	@Autowired
	private PanoramaNodeDeliverableService panoramaNodeDeliverableService;
	
	@Value("${panorama_nodes}")
	private String panoramaNodes;
	
	/**
	 * 全景计划-拿地时间(全景计划接口订阅)
	 * @param requestInfo
	 * @return
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public String panoramaPlan(JSONObject requestInfo) {
		//nodeDetail（节点明细）集合
		List<PanoramaNode>nodeDetailList=new ArrayList<>();
		//deliverableDetail （成果物明细）集合
		List<PanoramaNodeDeliverable>deliverableDetailList=new ArrayList<>();
		//全景计划主表
		Panorama panorama=new Panorama();
		
		JSONObject panoramicPlan = requestInfo.getJSONObject("panoramicPlan");
		JSONObject panoramicPlanInfoLine = panoramicPlan.getJSONObject("panoramicPlanInfoLine");
		
		//全景计划主表主键
		String projectPkId=UUIDUtils.create();
		String versionID= panoramicPlanInfoLine.getString("versionID");
		String projectID= panoramicPlanInfoLine.getString("projectID");
		String projectName = panoramicPlanInfoLine.getString("projectName");
		String pbnum= panoramicPlanInfoLine.getString("pbnum");
		String projectfID = panoramicPlanInfoLine.getString("projectfID");
		String projectfName = panoramicPlanInfoLine.getString("projectfName");
		String opnum = panoramicPlanInfoLine.getString("opnum");
		String batchID = panoramicPlanInfoLine.getString("batchID");
		String batchCode = panoramicPlanInfoLine.getString("batchCode");
		String batchName = panoramicPlanInfoLine.getString("batchName");
		String planName = panoramicPlanInfoLine.getString("planName");
		String versionType = panoramicPlanInfoLine.getString("versionType");
		int planVersion = panoramicPlanInfoLine.getInteger("planVersion");
		String areaID = panoramicPlanInfoLine.getString("areaID");
		String cityID = panoramicPlanInfoLine.getString("cityID");
		String cityLevel = panoramicPlanInfoLine.getString("cityLevel");
		String exWheather = panoramicPlanInfoLine.getString("exWheather");
		int undergroundNum = panoramicPlanInfoLine.getInteger("undergroundNum");
		int floorNum = panoramicPlanInfoLine.getInteger("floorNum");
		String prodType = panoramicPlanInfoLine.getString("prodType");
		String foundationType = panoramicPlanInfoLine.getString("foundationType");
		String preparation = panoramicPlanInfoLine.getString("preparation");
		String pitchedRoof = panoramicPlanInfoLine.getString("pitchedRoof");
		String existingHome = panoramicPlanInfoLine.getString("existingHome");
		String presaleNum = panoramicPlanInfoLine.getString("presaleNum");
		String rebuildCycle = panoramicPlanInfoLine.getString("rebuildCycle");
		String deliveryStandard = panoramicPlanInfoLine.getString("deliveryStandard");
		String holiday = panoramicPlanInfoLine.getString("holiday");
		String takeTime = panoramicPlanInfoLine.getString("takeTime");
		String isDeleted = panoramicPlanInfoLine.getString("isDeleted");
		String isFreeze= panoramicPlanInfoLine.getString("isFreeze");
		
		panorama.setId(projectPkId);
		panorama.setVersionId(versionID);
		panorama.setProjectId(projectID);
		panorama.setProjectName(projectName);
		panorama.setPbnum(pbnum);
		panorama.setProjectfId(projectfID);
		panorama.setProjectfName(projectfName);
		panorama.setOpnum(opnum);
		panorama.setBatchId(batchID);
		panorama.setBatchCode(batchCode);
		panorama.setBatchName(batchName);
		panorama.setPlanName(planName);
		panorama.setVersionType(versionType);
		panorama.setPlanVersion(planVersion);
		panorama.setAreaId(areaID);
		panorama.setCityId(cityID);
		panorama.setCityLevel(cityLevel);
		panorama.setExWheather(exWheather);
		panorama.setUndergroundNum(undergroundNum);
		panorama.setFloorNum(floorNum);
		panorama.setProdType(prodType);
		panorama.setFoundationType(foundationType);
		panorama.setPreparation(preparation);
		panorama.setPitchedRoof(pitchedRoof);
		panorama.setExistingHome(existingHome);
		panorama.setPresaleNum(presaleNum);
		panorama.setRebuildCycle(rebuildCycle);
		panorama.setDeliveryStandard(deliveryStandard);
		panorama.setHoliday(holiday);
		if(StringUtils.isNotBlank(takeTime)) {
			panorama.setTakeTime(LocalDateTimeUtils.timestamToDatetime(takeTime));
		}
		panorama.setIsDeleted(isDeleted);
		panorama.setIsFreeze(isFreeze);

		JSONArray nodeDetail = panoramicPlanInfoLine.getJSONArray("nodeDetail");
		for (Object nodeObject : nodeDetail) {
			//全景计划节点明细表
			
			JSONObject nodeObj = (JSONObject) nodeObject;
			String nodeCode = nodeObj.getString("nodeCode");
			
			boolean nodeFlag = isOrNot(nodeCode);
			
			if(!nodeFlag) { continue; }
			
			String nodePkId=UUIDUtils.create();
			String nodeVersionID = nodeObj.getString("versionID");
			String uniqueCode = nodeObj.getString("uniqueCode");
			String nodeName = nodeObj.getString("nodeName");
			String levelName = nodeObj.getString("levelName");
			String standardFinishDate = nodeObj.getString("standardFinishDate");
			String plannedFinishDate = nodeObj.getString("plannedFinishDate");
			String actualFinishDate = nodeObj.getString("actualFinishDate");
			int status = nodeObj.getInteger("status");
			String diffDate = nodeObj.getString("diffDate");
			String phaseName = nodeObj.getString("phaseName");
			String chargerID= nodeObj.getString("chargerID");
			String chargerName= nodeObj.getString("chargerName");
			String nodeIsDeleted= nodeObj.getString("isDeleted");
			String isSendBack= nodeObj.getString("isSendBack");
			
			PanoramaNode panoramaNode = new PanoramaNode();
			panoramaNode.setId(nodePkId);
			panoramaNode.setPkId(projectPkId);
			panoramaNode.setVersionId(nodeVersionID);
			panoramaNode.setUniqueCode(uniqueCode);
			panoramaNode.setNodeCode(nodeCode);
			panoramaNode.setNodeName(nodeName);
			panoramaNode.setLevelName(levelName);
			
			if(StringUtils.isNotBlank(standardFinishDate)) {
				panoramaNode.setStandardFinishDate(LocalDateTimeUtils.timestamToDatetime(standardFinishDate));
			}
			if(StringUtils.isNotBlank(plannedFinishDate)) {
				panoramaNode.setPlannedFinishDate(LocalDateTimeUtils.timestamToDatetime(plannedFinishDate));
			}
			if(StringUtils.isNotBlank(actualFinishDate)) {
				panoramaNode.setActualFinishDate(LocalDateTimeUtils.timestamToDatetime(actualFinishDate));
			}
			panoramaNode.setStatus(status);
			panoramaNode.setDiffDate(diffDate);
			panoramaNode.setPhaseName(phaseName);
			panoramaNode.setChargerId(chargerID);
			panoramaNode.setChargerName(chargerName);
			panoramaNode.setIsDeleted(nodeIsDeleted);
			panoramaNode.setIsSendBack(isSendBack);
			

			JSONArray deliverableDetail = nodeObj.getJSONArray("deliverableDetail");
			for (Object deliverableObject : deliverableDetail) {
				PanoramaNodeDeliverable panoramaNodeDeliverable=new PanoramaNodeDeliverable();
				JSONObject deliverableObj = (JSONObject) deliverableObject;
				String dlbVersionID = deliverableObj.getString("versionID");
				String dlbUniqueCode = deliverableObj.getString("uniqueCode");
				String deliverableName = deliverableObj.getString("deliverableName");
				String fileName = deliverableObj.getString("fileName");
				String fileUrl = deliverableObj.getString("fileUrl");
				
				panoramaNodeDeliverable.setId(UUIDUtils.create());
				panoramaNodeDeliverable.setVersionId(dlbVersionID);
				panoramaNodeDeliverable.setUniqueCode(dlbUniqueCode);
				panoramaNodeDeliverable.setDeliverableName(deliverableName);
				panoramaNodeDeliverable.setFileName(fileName);
				panoramaNodeDeliverable.setFileUrl(fileUrl);
				panoramaNodeDeliverable.setPkId(nodePkId);
				deliverableDetailList.add(panoramaNodeDeliverable);
			}
			nodeDetailList.add(panoramaNode);
		}
		panoramaNodeDeliverableService.saveBatch(deliverableDetailList);
		panoramaNodeService.saveBatch(nodeDetailList);
		int num=panoramaMapper.insert(panorama);		
		if (num>0) {
			return "插入成功";
		} else {
			return "插入失败";
		}
	}

	/**
	 * 判断是否需要该节点数据
	 * @param nodeCode
	 * @return
	 */
	private boolean isOrNot(String nodeCode) {
		boolean nodeFlag = false; 
		String [] panoramaNodesArr = panoramaNodes.split(",");
		for(String node : panoramaNodesArr) {
			node = node.trim();
			if(StringUtils.isEmpty(node)) {
				continue;
			}else if(StringUtils.isNotEmpty(node) && (nodeCode.equals(node))){
				nodeFlag = true;
				break;
			}else if(StringUtils.isNotEmpty(node) && (!nodeCode.equals(node))) {
				continue;
			}else {
				continue;
			}
		}
		return nodeFlag;
	}
	
}
